﻿using ImageLib;
using System;
using System.Net;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;

namespace ParserSkins
{
    public class SkinsParser : Parser
    {
        private string httpAnswer;
        public SkinsParser () {}
         
        public override void Parse(String webUrl)
        {
            imageCollection = new ImageItemCollection();
            httpAnswer = getPage(webUrl);
            parseAnswer(webUrl);
        }

        #region getters

        public override ImageItemCollection getImages()
        {
            return imageCollection;
        }

        public override string getName()
        {
            return "Skins Gallery Parser";
        }

        public override string getVersion()
        {
            return "1.0.0.0";
        }

        #endregion

        #region workers

        private void parseAnswer(String webUrl)
        {
            try
            {
                Regex div_regex = new Regex("<div class=\"userImage\">\\n(?<content>.*?)\\n</div>",RegexOptions.Multiline);
                MatchCollection div_matches = div_regex.Matches(httpAnswer);
                String div_subs;
                Regex img_regex = new Regex("<\\s*img(?<content>.*?)(/>|</)");
                MatchCollection img_matches;
                String img_subs;
                Regex src_regex = new Regex("src=(\"|\')(?<content>.*?)(\"|\')");
                Match src_Match;

                Console.WriteLine(div_matches.Count + " image tags found");
                for (int i = 0; i < div_matches.Count; i++)
                {
                    GalleryImageItem temp = new GalleryImageItem();
                    div_subs = div_matches[i].Groups[2].Value;

                    img_matches = img_regex.Matches(httpAnswer);
                    img_subs = img_matches[0].Groups[2].Value;

                    src_Match = src_regex.Match(img_subs);
                    #region src handling
                    try
                    {
                        UriBuilder uri = new UriBuilder(webUrl);
                        if (src_Match.Groups[3].Value.StartsWith("\\") || src_Match.Groups[3].Value.StartsWith("/"))
                        {
                            temp.ImgUrl = "http://" + uri.Host.ToString() + src_Match.Groups[3].Value;
                        }
                        else if (!src_Match.Groups[3].Value.StartsWith("http://") && !src_Match.Groups[3].Value.StartsWith("www."))
                        {
                            temp.ImgUrl = "http://" + uri.Host.ToString() + "//" + src_Match.Groups[3].Value;
                        }
                        else
                        {
                            temp.ImgUrl = src_Match.Groups[3].Value;
                        }
                    }
                    catch (Exception ex) { Console.WriteLine(ex.Message + i); }
                    #endregion

                    #region name handling
                    try
                    {
                        UriBuilder uri = new UriBuilder(temp.ImgUrl);
                        temp.Name = Path.GetFileName(uri.Path);
                    }
                    catch (Exception ex) { Console.WriteLine(ex.Message + i); }
                    #endregion

                    imageCollection.Add(temp);

                    Console.WriteLine("Tag " + i + " :");
                    Console.WriteLine("\t SRC : " + temp.ImgUrl);
                    Console.WriteLine("\t NAM : " + temp.Name);
                }
            }
            catch (Exception ex) { }
        }

        #endregion
    }
}
